﻿using DAL.Helper;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DAL
{
    public abstract class BaseDaoPro<T> where T : class, new()
    {
        protected SqlSugarClient db = SqlSugarHelper.getConnection();

        public virtual bool Insert(T model)
        {
            return db.Insertable<T>(model).ExecuteCommandIdentityIntoEntity();
        }

        public virtual bool Update(T model)
        {
            return db.Updateable<T>(model).ExecuteCommandHasChange();
        }

        public virtual bool Delete(int[] ids)
        {
            return db.Deleteable<T>().In(ids).ExecuteCommandHasChange();
        }

        public virtual List<T> QueryForList(List<IConditionalModel> searchParams, ref int totalCount, int pageIndex = 1, int pageSize = 15)
        {
            return db.Queryable<T>()
                .Where(searchParams)
                .ToPageList(pageIndex, pageSize, ref totalCount);
        }
        public virtual List<T> QueryForList(List<IConditionalModel> searchParams, string orderBy, ref int totalCount, int pageIndex = 1, int pageSize = 15)
        {
            return db.Queryable<T>()
                .Where(searchParams)
                .OrderBy(orderBy)
                .ToPageList(pageIndex, pageSize, ref totalCount);
        }

        public virtual T QueryForOne(List<IConditionalModel> searchParams)
        {
            return db.Queryable<T>()
                .Where(searchParams)
                .First();
        }

    }
}
